package com.cipe.cmrs.model;

import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;

import com.cipe.cmrs.model.ref.Link;

/**
 * <p>
 * Pojo mapping TABLE dbo.Role
 * </p>
 * 
 * <p>
 * Generated at Sat Dec 03 14:39:50 PKT 2011
 * </p>
 * 
 * @author Incepio
 * 
 */
@Entity
@Table(name = "Role")
@SuppressWarnings("serial")
public class Role extends BaseAuditEntity implements GrantedAuthority {

    /**
     * Attribute displayName.
     */
    @Basic
    @Column(name = "rolename", length = 100)
    private String displayName;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "roleprivileges", joinColumns = { @JoinColumn(name = "roleid") }, inverseJoinColumns = @JoinColumn(name = "link"))
    private Set<Link> links;

    @Transient
    public String getAuthority() {
        return displayName;
    }

    /**
     * @return displayName
     */
    public String getDisplayName() {
        return displayName;
    }

    @Transient
    public String getShortDisplayName() {
        if (displayName != null) {
	        String name = new String(displayName);
	        if (name.startsWith("ROLE_")) {
	        	name = name.substring(5);
	        }
	        if (name.indexOf('_') > -1) {
	        	name = name.replace('_', ' ');
	        }
	        if (name.indexOf("SENIOR") > -1) {
	        	name = name.replace("SENIOR", "SR.");
	        }
	        if (name.indexOf("MANAGEMENT") > -1) {
	        	name = name.replace("MANAGEMENT", "MGMT.");
	        }
	        return name;
        }
        return null;
    }

    /**
     * @param displayName
     *            new value for displayName
     */
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public Set<Link> getLinks() {
        return links;
    }

    public void setLinks(Set<Link> links) {
        this.links = links;
    }

}